package com.kun.kunkunmall.ware.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MyMQ {

    /**
     * 库存服务的交换机
     * @return
     */
    @Bean
    public Exchange stockEventExchange(){
        return ExchangeBuilder.topicExchange("stock-event-exchange").durable(true).build();
    }

    /**
     * 接受死信的队列
     * @return
     */
    @Bean
    public Queue stockReleaseStockQueue(){
        return QueueBuilder.durable("stock.release.stock.queue").build();
    }

    /**
     * 库存服务的延迟队列
     * @return
     */
    @Bean
    public Queue stockDelayQueue(){
        return QueueBuilder.durable("stock.delay.queue").ttl(120000)
                        .deadLetterRoutingKey("stock.release").deadLetterExchange("stock-event-exchange").build();
    }

    @Bean
    public Binding stockReleaseBinding(@Qualifier("stockReleaseStockQueue") Queue queue,@Qualifier("stockEventExchange")Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with("stock.release").noargs();
    }

    @Bean
    public Binding stockLockedBinding(@Qualifier("stockDelayQueue")Queue queue,@Qualifier("stockEventExchange")Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with("stock.locked").noargs();
    }

}
